﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PythagoreanTheoremControl
{
    /// <summary>
    /// Interaction logic for MyCanvas.xaml
    /// </summary>
    public partial class MyCanvas : UserControl
    {

        Point StartPoint;
        Point EndPoint;
        Line CurrentLine;

        public MyCanvas()
        {
            InitializeComponent();
            myCanvas.MouseLeftButtonDown += new MouseButtonEventHandler(myCanvas_MouseLeftButtonDown);
            myCanvas.MouseMove += new MouseEventHandler(myCanvas_MouseMove);
            myCanvas.MouseLeftButtonUp += new MouseButtonEventHandler(myCanvas_MouseLeftButtonUp);
        }

        void myCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StartPoint = e.GetPosition(myCanvas);

            CurrentLine = new Line();
            CurrentLine.StrokeEndLineCap = PenLineCap.Round;
            CurrentLine.StrokeStartLineCap = PenLineCap.Round;
            CurrentLine.Stroke = Brushes.Red;
            CurrentLine.StrokeThickness = 2.0;
            CurrentLine.X1 = StartPoint.X;
            CurrentLine.Y1 = StartPoint.Y;
            CurrentLine.X2 = StartPoint.X;
            CurrentLine.Y2 = StartPoint.Y;
            myCanvas.Children.Add(CurrentLine);
        }
        void myCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            EndPoint = e.GetPosition(myCanvas);
            if (CurrentLine != null)
            {
                CurrentLine.X2 = EndPoint.X;
                CurrentLine.Y2 = EndPoint.Y;
            }
        }

        void myCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Line leaveBehind = new Line();
            leaveBehind.StrokeEndLineCap = PenLineCap.Round;
            leaveBehind.StrokeStartLineCap = PenLineCap.Round;
            leaveBehind.Stroke = Brushes.Red;
            leaveBehind.StrokeThickness = 2.0;

            leaveBehind.X1 = CurrentLine.X1;
            leaveBehind.Y1 = CurrentLine.Y1;
            leaveBehind.X2 = e.GetPosition(myCanvas).X;
            leaveBehind.Y2 = e.GetPosition(myCanvas).Y;
            myCanvas.Children.Add(leaveBehind);
            myCanvas.Children.Remove(CurrentLine);

            double xDistance = Math.Round(StartPoint.X - EndPoint.X);
            double yDistance = Math.Round(StartPoint.Y - EndPoint.Y);
           
            //PythagoreanTheorem (a*a) + (b*b) = (c*c)
            double totalDistance = Math.Round(Math.Sqrt((xDistance * xDistance) + (yDistance * yDistance)));


            Label l = new Label();
            l.Content = totalDistance.ToString();
            myCanvas.Children.Add(l);
            Canvas.SetLeft(l, EndPoint.X);
            Canvas.SetTop(l, EndPoint.Y);
        }

    }
}
